package com.heima.service.Impl;

import com.heima.dao.AccountDao;
import com.heima.pojo.Account;
import com.heima.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.sql.DataSource;

@Service
@Transactional
public class AccountServiceImpl implements AccountService {

    //自动装载
    @Autowired
    AccountDao accountDao;



    @Override
    public void transfer(String fromName, String toName, double money) {
        //根据名称获取账户对象
        Account fromAccount = accountDao.findByName(fromName);
        Account toAccount = accountDao.findByName(toName);
        //开始转账
        fromAccount.setMoney(fromAccount.getMoney() - money);
        //System.out.println(1/0);
        toAccount.setMoney(toAccount.getMoney() + money);
        //转账完成后更新账户
        accountDao.update(fromAccount);
        accountDao.update(toAccount);
    }
}
